package top.huxy.boot.config.argumentResolver;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import top.huxy.boot.model.dto.Query;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @Classname QueryArgumentResolver
 * @Description QueryArgumentResolver
 * @Date 2021/9/26 17:22
 * @Created by huxy
 */
@Slf4j
public class QueryArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(Query.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        Iterator<String> parameterNames = webRequest.getParameterNames();
        HashMap<String, String> map=new HashMap<>();
        while(parameterNames.hasNext()){
            String name = parameterNames.next();
            map.put(name,webRequest.getParameter(name));
        }

        return new Query(map);
    }
}
